package com.example.intelligentattendancesystem.cron;

import com.example.intelligentattendancesystem.mapping.HistoryMapping;
import com.example.intelligentattendancesystem.mapping.LeaveMapping;
import com.example.intelligentattendancesystem.mapping.WorkMapping;
import com.example.intelligentattendancesystem.pojo.History;
import com.example.intelligentattendancesystem.pojo.Leave;
import com.example.intelligentattendancesystem.pojo.User;
import com.example.intelligentattendancesystem.pojo.Work;
import com.example.intelligentattendancesystem.service.AbsenceService;
import com.example.intelligentattendancesystem.service.SignService;
import com.example.intelligentattendancesystem.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @className Task
 * @since 1.0
 */
@Component
public class Task {
    @Autowired
    AbsenceService absenceService;
    @Autowired
    WorkService workService;
    @Autowired
    WorkMapping workMapping;
    @Autowired
    HistoryMapping historyMapping;
    @Autowired
    LeaveMapping leaveMapping;
    //每天23点32分55秒运行
    @Scheduled(cron="55 32 23 * * ?")
//    @Scheduled(cron="0/15 * *  * * ? ")
    public void execute(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
//        System.out.println("欢迎访问 pan_junbiao的博客 " + df.format(new Date()));
        String currentTime=df.format(new Date());
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week = cal.get(Calendar.DAY_OF_WEEK)-1;
        if(week==0){
            week=7;
        }
//        System.out.println(week);
        String today=currentTime.split(" ")[0];
//        System.out.println(today);
        //先查询所有的班次,再查询班次里面的员工列表,再查询列表的员工是否在每一班里面有打卡记录,没有就是缺勤
        List<Work> tableService = workMapping.selectWorkByUserId(11);
//        System.out.println("tableservice:"+tableService);
        for(int j=0;j<tableService.size();j++) {
            String startTime = tableService.get(j).getStartTime();
            String endTime = tableService.get(j).getEndTime();
            //获取每一个时间段的员工列表
            List<User> workerListService = workService.getWorkerListService(startTime, endTime, String.valueOf(week));
//            System.out.println("worklistservice:"+week+","+workerListService);
            for (int k = 0; k < workerListService.size(); k++) {
//                System.out.println("user:"+k+","+workerListService.get(k));
                int flag = 0;
                List<History> histories = historyMapping.selectHistoryByUserIdInToday(workerListService.get(k).getUserId(), today);
                for (int l = 0; l < histories.size(); l++) {
                    System.out.println("history:"+histories.get(l));
                    if (histories.get(l).getSignOutTime().equals(today+" "+endTime)) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) {
                    int f=0;
                    List<Leave> leaves = leaveMapping.selectLeaveByUserId(workerListService.get(k).getUserId());
                    if(leaves!=null){
                        for (Leave leave :
                                leaves) {
                            if(leave.getLeaveStartTime().equals(startTime)){
                                f=1;
                                break;
                            }
                        }
                    }
                    if(f!=1){
                        absenceService.addAbsenceService(workerListService.get(k).getUserId(),startTime,endTime,currentTime);
                    }
//                    System.out.println("缺勤记录： " + workerListService.get(k).getUserId() +","+ startTime + ","+endTime);
                }
            }
        }
    }
}
